var active = {
    //初始化轮播图
    initBanner: function () {
        $.ajax({
            url: "/interface/bannerListAll",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.returnCode === "00") {
                    var host = location.host;
                    var list = res.data;
                    if (!!list && list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            var str = '<li data-title="" class="hiSlider-item"><img src="http://' + host + list[i].IMAGE_ADDRESS + '" alt="" width="870px" height="556px"></li>';
                            $(".hiSlider").append(str);
                        }
                    }
                    $('.hiSlider').hiSlider({
                        intervalTime: 3000,
                        isShowTitle: false,
                        isShowControls: false
                    });
                } else if (res.returnCode === "01") {
                    console.log(res);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    //初始化交易数据
    initTransaction: function () {
        $.ajax({
            url: "/interface/get4DataAndStatistics",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.returnCode === "00") {

                    var data = res.statisticsData;
                    if (!!data) {
                        $(".all-number").html(data.ALL_NUMBER.toLocaleString());
                        $(".all-financing-income").html(active.thousandBitSeparator(Number(data.ALL_FINANCING_INCOME).toFixed(2)));
                        $(".all-aggregate-amount").html(active.thousandBitSeparator(Number(data.ALL_AGGREGATE_AMOUNT).toFixed(2)));
                    }

                    var list = res.list4Data;
                    if (!!list && list.length > 0) {
                        for (var i = 0; i < list.length; i++) {
                            var cj = '';
                            if (list[i].IS_DEALED == 1) {
                                cj = '<div class="deal-icon"><img src="../../static/images/ysh/deal-icon.jpg" alt=""></div>\n';
                            }
                            var str = '<div class="ysh-hot-Trade-list fl">\n' +
                                '          <div class="ysh-hot-Trade-list-main">\n' +
                                '              <div class="ysh-hot-Trade-list-title clearfix">\n' +
                                '                  <span class="name fl">' + list[i].PRODUCT_NAME + '</span>\n' +
                                '                  <span class="date fr">' + active.toDateString(list[i].TRANSACTION_DATE, "MM-dd") + '</span>\n' +
                                '              </div>\n' +
                                '              <div class="ysh-hot-Trade-list-text">\n' +
                                '                  <p class="quantity"><span>' + list[i].NUMBER + '</span>吨</p>\n' +
                                '                  <p class="source">来源：' + list[i].SELLER + '</p>\n' +
                                cj +
                                '              </div>\n' +
                                '          </div>\n' +
                                '      </div>';
                            $(".ysh-hotTrade-main").append(str);
                        }
                    }

                } else if (res.returnCode === "01") {
                    console.log(res);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    //初始化资讯列表
    initInformationList: function () {
        // 获取栏目列表
        $.ajax({
            url: "/interface/getColumnList",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.returnCode === "00") {
                    $(".ysh-mes-column").empty();
                    var columnList = res.list;
                    if (!!columnList && columnList.length > 0) {
                        for (var j = 0; j < 3; j++) {
                            $.ajax({
                                url: "/interface/getInformationList",
                                data: {COLUMN_NAME: columnList[j].COLUMN_NAME, page: 1, limit: 10},
                                dataType: "json",
                                type: "post",
                                async: false,
                                success: function (res) {
                                    if (res.returnCode === "00") {
                                        var list = res.data;
                                        if (!!list && list.length > 0) {
                                            var len = list.length;
                                            if (len > 6) {
                                                len = 6;
                                            }
                                            var str = '<div class="ysh-mes-list fl">\n' +
                                                '                <div class="ysh-mes-list-title">\n' +
                                                '                    <h4>' + columnList[j].COLUMN_NAME + '<a href="list.html?type=' + columnList[j].ID + '" target="_blank"><img src="../../static/images/ysh/more-icon.png" alt=""></a></h4>\n' +
                                                '                </div>\n' +
                                                '                <div class="ysh-mes-list-main">\n';

                                            for (var i = 0; i < len; i++) {
                                                if (i == 0) {
                                                    var host = location.host;
                                                    var src = "http://" + host + list[i].IMAGE_ADDRESS;
                                                    str += '<div class="ysh-mes-list-main-top">\n' +
                                                        '      <a href="detail.html?id=' + list[i].ID + '" target="_blank">\n' +
                                                        '          <p><img src="' + src + '" alt="' + list[i].TITLE + '" width="290px" height="120px"></p>\n' +
                                                        '          <p class="img-title">' + list[i].TITLE + '</p>\n' +
                                                        '      </a>' +
                                                        '   </div>\n' +
                                                        '   <ul class="ysh-mes-list-main-btm">\n';
                                                } else {
                                                    str += '<li><a href="detail.html?id=' + list[i].ID + '" target="_blank">' + list[i].TITLE + '</a></li>';
                                                }
                                            }
                                            str  += '       </ul>\n' +
                                                '       </div>\n' +
                                                '    </div>';
                                            $(".ysh-mes-column").append(str);
                                        }
                                    } else if (res.returnCode === "01") {
                                        console.log(res);
                                    }
                                },
                                error: function (XMLHttpRequest, textStatus, errorThrown) {
                                    console.error(XMLHttpRequest.status);
                                    console.error(XMLHttpRequest.readyState);
                                    console.error(textStatus);
                                }
                            });
                        }
                    }

                } else if (res.returnCode === "01") {
                    console.log(res);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
    },
    //初始化折线图
    initEchart: function () {
        var bltDates = [];
        var bltvalues = [];
        var ldDates = [];
        var ldvalues = [];
        $.ajax({
            url: "/interface/getPriceTrend",
            data: {},
            dataType: "json",
            type: "post",
            async: false,
            success: function (res) {
                if (res.returnCode === "00") {
                    bltDates = res.data.bltDates;
                    bltvalues = res.data.bltvalues;
                    ldDates = res.data.ldDates;
                    ldvalues = res.data.ldvalues;
                } else if (res.returnCode === "01") {
                    console.log(res);
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.error(XMLHttpRequest.status);
                console.error(XMLHttpRequest.readyState);
                console.error(textStatus);
            }
        });
        // 基于准备好的dom，初始化echarts实例
        var bltChart = echarts.init(document.getElementById('bltChart'));
        var ldChart = echarts.init(document.getElementById('ldChart'));
        var option = {
            title: {
                text: '布伦特原油指数',
                textStyle: {
                    //文字颜色
                    color: '#555555',
                    //字体风格,'normal','italic','oblique'
                    fontStyle: 'normal',
                    //字体粗细 'normal','bold','bolder','lighter',100 | 200 | 300 | 400...
                    fontWeight: 'normal',
                    //字体大小
                    fontSize: 20
                }
            },
            tooltip: {
                trigger: 'axis'
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            grid: {
                left: '0%',
                right: '15%',
                bottom: '5%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    boundaryGap: false,
                    axisLabel: {
                        "interval": 0,
                        "rotate": 70,
                        "show": true,
                        "splitNumber": 15,
                        "textStyle": {
                            "fontFamily": "微软雅黑",
                            "fontSize": 12
                        }
                    },
                    data: bltDates.map(function (str) {
                        return str.replace(' ', '\n')
                    })
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                    name: '价格',
                    type: 'line',
                    hoverAnimation: false,
                    itemStyle: {
                        normal: {
                            color: '#32d0ff',
                            lineStyle: {
                                color: '#32D0FF'
                            }
                        }
                    },
                    data: bltvalues
                }
            ]
        };
        var option1 = {
            title: {
                text: '伦敦原油指数',
                textStyle: {
                    //文字颜色
                    color: '#555555',
                    //字体风格,'normal','italic','oblique'
                    fontStyle: 'normal',
                    //字体粗细 'normal','bold','bolder','lighter',100 | 200 | 300 | 400...
                    fontWeight: 'normal',
                    //字体大小
                    fontSize: 20
                }
            },
            tooltip: {
                trigger: 'axis'
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            grid: {
                left: '0%',
                right: '15%',
                bottom: '5%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    boundaryGap: false,
                    axisLabel: {
                        "interval": 0,
                        "rotate": 70,
                        "show": true,
                        "splitNumber": 15,
                        "textStyle": {
                            "fontFamily": "微软雅黑",
                            "fontSize": 12
                        }
                    },
                    data: ldDates.map(function (str) {
                        return str.replace(' ', '\n')
                    })
                }
            ],
            yAxis: [
                {
                    type: 'value'
                }
            ],
            series: [
                {
                    name: '价格',
                    type: 'line',
                    hoverAnimation: false,
                    itemStyle: {
                        normal: {
                            color: '#32d0ff',
                            lineStyle: {
                                color: '#32D0FF'
                            }
                        }
                    },
                    data: ldvalues
                }
            ]
        };
        // 使用刚指定的配置项和数据显示图表。
        bltChart.setOption(option);
        ldChart.setOption(option1);
    },
    //时间戳的处理
    toDateString: function (d, format) {
        var date = new Date(d || new Date())
            , ymd = [
            active.digit(date.getFullYear(), 4)
            , active.digit(date.getMonth() + 1)
            , active.digit(date.getDate())
        ]
            , hms = [
            active.digit(date.getHours())
            , active.digit(date.getMinutes())
            , active.digit(date.getSeconds())
        ];

        format = format || 'yyyy-MM-dd HH:mm:ss';

        return format.replace(/yyyy/g, ymd[0])
            .replace(/MM/g, ymd[1])
            .replace(/dd/g, ymd[2])
            .replace(/HH/g, hms[0])
            .replace(/mm/g, hms[1])
            .replace(/ss/g, hms[2]);
    },
    //数字前置补零
    digit: function (num, length, end) {
        var str = '';
        num = String(num);
        length = length || 2;
        for (var i = num.length; i < length; i++) {
            str += '0';
        }
        return num < Math.pow(10, length) ? str + (num | 0) : num;
    },
    //千位符
    thousandBitSeparator: function (num) {
        return num && num
            .toString()
            .replace(/(\d)(?=(\d{3})+\.)/g, function ($0, $1) {
                return $1 + ",";
            });
    }
};

$(function () {
    active.initBanner();
    active.initTransaction();
    active.initInformationList();
    active.initEchart();
});
